<?php

namespace app;

use thans\jwt\exception\JWTException;
use thans\jwt\facade\JWTAuth;

class CheckTokenController extends BaseController
{
    // 获取请求头
    protected function initialize()
    {
        parent::initialize();
        // 判断有没有token
        $header = $this->request->header();
        if (!isset($header['authorization'])) {
            return result([], '请先登录', 401)->send();
        }
        if (empty($header['authorization'])) {
            return result([], 'token不能为空，请先登录', 401)->send();
        }

        // 解析token

        $payload = JWTAuth::auth(); //可验证token, 并获取token中的payload部分
        $uid = $payload['uid']->getValue(); //可以继而获取payload里自定义的字段，比如uid

        // 往下传递用户信息
        $this->uid = $uid;
    }
}
